﻿if( window.jQuery ) {
( function($) {
	/**
	 * 
	 * Version 1.7
	 * 
	 * 
	**/

	jQuery.extend({
		/**
		 *
			Fixed old open container
		 *
		 **/
		jfinput_open: {}
	});

	jQuery.fn.jfinput_load_json = function( jo, uv, old, val ) {
		var e = $(this);

		if ( !jo ) { return false; }
		if ( typeof jo == 'string') { jo = { url: jo, data: {} } }
		else { if ( typeof jo != 'object' || jo.url == 'undefined' || !jo.url ) return false; }

		var p = {};
		jQuery.extend( p, jo );
		jQuery.extend( p.data, {
			user_value: uv,
			id: e.attr('id'),
			type: e.attr('type'),
			name: e.attr('name'),
			old_value: old,
			view_value: val
		});

		var us = ( p.success && typeof p.success == 'function' ? p.success : function (){} );
		if ( p.dataType == 'text' ) {
			e.load( p.url, p.data, us );
		}
		else { /* "xml" - "html" - "script" - "json" - "jsonp" - "text" */
			jQuery.data(e.get(0), '_jfinput_load_json', us );
			p.success = function( data, st ) { 
				e.jfinput_load_json_tmpl = jQuery.data( e.get(0), '_jfinput_load_json' );
				e.jfinput_load_json_tmpl( data, st );
				jQuery.removeData( e.get(0), '_jfinput_load_json' );
				e.jfinput_load_json_tmpl = null;
			};
			jQuery.ajax( p );
		}

		return true;
	};

	jQuery.fn.jfinput = function( input ) {
		return jQuery.each( $(this), function(){ $(this)._jfinput( input ); });
	};

	jQuery.fn._jfinput = function( input ) {
			/*
				style: null,
				prompting: null,
				size: ,
				maxlength,
				css: {},
				prompting_value: null,
				callback: function(a,b,c){},
				view_call: function(a,b,c,d){ var s=new RegExp("\n","ig"); return String(d).replace( s, '<br />' ); },
				ajax_call_data: null
			*/
		var o = {
			rows: 10,
			cols: 10,
			type: 'input' /* or "textarea" or 'password' */
		};

		var e = $(this);
		var id = e.attr('id');
		if (!id || id == 'undefined' || id == undefined ) e.attr('id', (new Date()).getTime() +'jfinput'+ Math.random() );


		if ( input && typeof input == 'object' ) {
			jQuery.extend( o, input );
			o.css = input['css'] ? input['css'] : {};
			o.callback = input['callback'] && typeof input.callback == 'function' ? input.callback : function(a,b,c){};
			o.view_call = input['view_call'] && typeof input.view_call == 'function' ?
				input.view_call : function(a,b,c,d){ var s=new RegExp("\n","ig"); return String(d).replace( s, '<br />' ); };
		}

		var prom = String(o['prompting'] || '');
		var prom_value = String(o['prompting_value'] || '');
		var val = e.html();
		if ( !val || val == '' || val == 'undefined'  ) 
			var val = ( prom && prom != '' && e.attr(prom) != 'undefined' ? e.attr(prom) : prom_value ) || '';

		if ( o['view_call'] ) val = o['view_call']( '', e.attr('id'), val, val, true );

		e.text( val );
		e.click(function () { 
			var e = $(this);

			for ( var t in jQuery.jfinput_open ) {
				if ( t != e.attr('id') ) {
					$('#'+ t).trigger( 'mychange', [ 'setTimeout' ] );
					jQuery.jfinput_open[t] = null;
					delete jQuery.jfinput_open[t];
				}
			}

			jQuery.jfinput_open[ e.attr('id') ] = 1;

			if ( jQuery.data( e.get(0), 'jfinput') ) return;
			jQuery.data( e.get(0), 'jfinput', true );
			jQuery.data( e.get(0), 'jfinputvalue', e.html() );

			var id = e.attr('id') +'_'+ new Date().getTime() + Math.random();

			var t;
			var st = ( o['style'] ? ' style="'+ o['style'] +'" ' : '' );
			var ml = ( o['maxlength'] && parseInt(o['maxlength'], 10) > 0 ? ' maxlength="'+ parseInt(o['maxlength'], 10) +'" ' : '' );
			var sz = ( o['size'] && parseInt(o['size'], 10) > 0 ? ' size="'+ parseInt(o['size'], 10) +'" ' : '' );

			if ( 'input' == o.type )
				t = $('<input type="text" '+ st + ml + sz +' id="'+ id +'" value="'+ e.html() +'">');
			else if ( 'password' == o.type )
				t = $('<input type="password" '+ st + ml + sz +' id="'+ id +'" value="">');
			else if ( 'textarea' == o.type )
				t = $('<textarea id="'+ id +'" ' 
					+( o['rows'] && 0 < o['rows'] ? 'rows="'+ o.rows +'" ' : '' )
					+( o['cols'] && 0 < o['cols'] ? 'cols="'+ o.cols +'" ' : '' )
					+ st
					+'">'+ e.html() +'</textarea>');
			else 
				return;

			for ( var s in o.css ) t.css( s, o.css[s] );
			e.empty().append(t).focus();
			t.focus();

			var ajax_call_data = o['ajax_call_data'];
			var prompting = String(o['prompting'] || '');
			var prompting_value = String(o['prompting_value'] || '');
			var view_call = o.view_call;
			var callback = o.callback;

			t.one( 'mychange', function ( e, from ) {
				var e = $(this);
				var p = e.parent();

				delete jQuery.jfinput_open[ e.attr('id') ];

				var view_value = uv = e.val();

				if ( e.is('input') && e.attr('type') == 'password' )
					var s=new RegExp(".","ig"); view_value = String(view_value).replace( s, '*' );

				if ( !view_value || view_value == null || view_value == '' ) 
					view_value = ( prompting && prompting != '' && p.attr(prompting) != 'undefined' ? 
							p.attr(prompting) : prompting_value ) || '';
				e.remove();

				var old = jQuery.data( p.get(0), 'jfinputvalue' );
				if ( view_call && from == 'setTimeout' ) view_value = view_call( uv, p.attr('id'), old, view_value, false );

				jQuery.removeData( p.get(0), 'jfinput' );
				jQuery.removeData( p.get(0), 'jfinputvalue' );

				if ( from == 'setTimeout') {
					if ( !ajax_call_data ) {
						p.html(view_value);
						if ( callback ) callback( uv,  p.attr('id'), old, view_value );
					} else {
						p.jfinput_load_json( ajax_call_data, uv, old, view_value );
					}
				} else 
					p.html(view_value);
			});

			t.blur( function () {
				setTimeout( function (e){ if ( t && t.length ) t.trigger( 'mychange', [ 'setTimeout' ] ); }, 1, $(this) );
			}).keypress( function(event){
				if ( event.keyCode == 27 ) {
					var old = jQuery.data( $(this).parent().get(0), 'jfinputvalue' );
					$(this).val(old);
					$(this).trigger( 'mychange', [ 'Esc' ] );
				} else if ( event.keyCode == 13 && $(this).is('input') ) {
					$(this).trigger( 'mychange', [ 'setTimeout' ] );
				}
				return true;
			});
		});
	};
})(jQuery);}